package com.tjtt.tdm.portlet.dao.impl;

import java.util.List;
import java.util.Map;

import org.hibernate.Query;

import com.tjtt.tdm.base.BaseHibernateDao;
import com.tjtt.tdm.base.PageResult;
import com.tjtt.tdm.portlet.bean.SysPortlet;
import com.tjtt.tdm.portlet.dao.PortletDao;

public class PortletDaoImpl extends BaseHibernateDao implements PortletDao{

	
	public List getPortletList(String sql) {
		Query query = getSession().createSQLQuery(sql);
		return query.list();
	}

	
	public List<SysPortlet> getAllPortlet() {
		List<SysPortlet> list = this.getHibernateTemplate().find("from SysPortlet");
		return list;
	}

	
	public List<SysPortlet> getPortletByids(String ids) {
		List<SysPortlet> list = this.getHibernateTemplate().find("from SysPortlet s where  s.state != '停用' and  s.id in("+ids+") order by s.location asc, s.portletSort asc");
		return list;
	}
	public PageResult queryPortletList(Map filterMap){
		StringBuffer queryString = new StringBuffer();
		queryString.append("SELECT t.id,t.portlet_Name,t.portlet_Url,t.execute_Type,t.portlet_Sql," +
							"t.remarks,t.state,t.portlet_Height,t.localtion,t.portlet_sort " +
							"from sys_portlet t where 1=1 ");
		StringBuffer countString = new StringBuffer();
		countString.append("SELECT COUNT(*) FROM sys_portlet t where 1=1 ");
		if (filterMap != null && !filterMap.isEmpty()) {
			String filterProperty;
			String filterValue;
			
//			filterProperty = "lhProjectNo";
//			filterValue = (String) filterMap.get(filterProperty);
//			if (!StringUtil.isMissing(filterValue)) {
//				queryString.append(" and t.l_project_no").append(" like '%").append(filterValue).append("%'");
//				countString.append(" and t.l_project_no").append(" like '%").append(filterValue).append("%'");
//			} 
//			filterProperty = "lhProjectName";
//			filterValue = (String) filterMap.get(filterProperty);
//			if (!StringUtil.isMissing(filterValue)) {
//				queryString.append(" and t.L_PROJECT_NAME").append(" like '%").append(filterValue).append("%'");
//				countString.append(" and t.L_PROJECT_NAME").append(" like '%").append(filterValue).append("%'");
//			} 
//			filterProperty = "lTypeId";
//			filterValue = (String) filterMap.get(filterProperty);
//			if (!StringUtil.isMissing(filterValue)) {
//				queryString.append(" and t.L_TYPE_ID").append(" ='").append(filterValue).append("'");
//				countString.append(" and t.L_TYPE_ID").append(" ='").append(filterValue).append("'");
//			} 
			
			//String sidx = (String)filterMap.get("sidx");
			//String sord = (String)filterMap.get("sord");
			//queryString.append(" order by t." + sidx + " " + sord );
		}
		return searchBySQL(countString.toString(),queryString.toString(),filterMap);
	
	}
	public void savePortlet(SysPortlet portlet)
	{
		this.getHibernateTemplate().save(portlet);
	}
	public void deletePortlet(String ids)
	{
		String[] idss = ids.split(",");
		for(String id : idss){
			SysPortlet lpi = (SysPortlet)this.getHibernateTemplate().get(SysPortlet.class, Long.parseLong(id));
			this.getHibernateTemplate().delete(lpi);
		}
	}
	public SysPortlet GetPortletById(String id)
	{
		SysPortlet lpi = (SysPortlet)this.getHibernateTemplate().get(SysPortlet.class, Long.parseLong(id));
		return lpi;
	}
	public void updatePortlet(SysPortlet portlet)
	{
		this.getHibernateTemplate().update(portlet);
	}
}
